package com.dlc.garbagerecyclebox.core;

import android.content.Context;

import com.dlc.commonbiz.base.exception.ApiException;
import com.dlc.commonbiz.base.exception.DlcMqttException;
import com.dlc.commonbiz.base.mqtt.bean.DlcMqttConnectOptions;
import com.dlc.commonbiz.base.mqtt.bean.DlcMqttToken;
import com.dlc.commonbiz.base.mqtt.callback.BaseMqttCallBack;
import com.dlc.commonbiz.base.mqtt.core.MqttMgr;
import com.dlc.commonbiz.base.serialport.bean.InitSerialPortGrbBean;
import com.dlc.garbagerecyclebox.GarbageRecyclerBoxCmdProxy;
import com.dlc.garbagerecyclebox.bean.GarbageRecyclerBoxBean;
import com.dlc.garbagerecyclebox.callback.GarbageRecyclerBoxCmdCallback;
import com.dlc.garbagerecyclebox.callback.GarbageRecyclerBoxProxyCallback;
import com.dlc.garbagerecyclebox.exception.GarbageRecyclerBoxExceptionCode;

/**
 * @author :      fangbingran
 * @aescription : 回收箱子代理
 * @date :        2019/08/03  11:12
 */
public class GarbageRecyclerBoxProxy {
    private static class InstanceGarbageRecyclerBoxToMqttProxy {
        private static final GarbageRecyclerBoxProxy INSTANCE = new GarbageRecyclerBoxProxy();
    }

    public static GarbageRecyclerBoxProxy get() {
        return InstanceGarbageRecyclerBoxToMqttProxy.INSTANCE;
    }

    private boolean isMqttSuccess = false;
    private GarbageRecyclerBoxProxyCallback mCallback;
    private InitSerialPortGrbBean initSerialPortGrbBean;
    private InitSerialPortGrbBean initVisionSerialPortGrbBean;

    public void init(final Context context, final GarbageRecyclerBoxBean garbageRecyclerBoxBean, final GarbageRecyclerBoxProxyCallback callback) {
        if (garbageRecyclerBoxBean == null) {
            throw new ApiException(GarbageRecyclerBoxExceptionCode.GARBAGE_RECYCLER_BOX_ERROR, "GarbageRecyclerBoxBean不能为空");
        }
        mCallback = callback;
        initSerialPortGrbBean = garbageRecyclerBoxBean.getInitSerialPortGrbBean();
        initVisionSerialPortGrbBean = garbageRecyclerBoxBean.getInitVisionSerialPortGrbBean();
        GarbageRecyclerBoxCmdProxy.get().init(context, garbageRecyclerBoxBean.getInitSerialPortGrbBean(),
                garbageRecyclerBoxBean.getInitVisionSerialPortGrbBean(), mCallback);
        initMqtt(context, garbageRecyclerBoxBean.getDlcMqttConnectOptions(), callback);
    }

    public void init(final Context context, InitSerialPortGrbBean initSerialPortGrbBean, final DlcMqttConnectOptions dlcMqttConnectOptions, final GarbageRecyclerBoxProxyCallback callback) {
        if (initSerialPortGrbBean == null) {
            throw new ApiException(GarbageRecyclerBoxExceptionCode.GARBAGE_RECYCLER_BOX_ERROR, "initSerialPortGrbBean不能为空");
        }
        mCallback = callback;
        GarbageRecyclerBoxCmdProxy.get().init(context, initSerialPortGrbBean, mCallback);
        initMqtt(context, dlcMqttConnectOptions, callback);
    }

    public void initMqtt(final Context context, DlcMqttConnectOptions dlcMqttConnectOptions, final GarbageRecyclerBoxProxyCallback callback) {
        MqttMgr.get().init(context, dlcMqttConnectOptions
                , new BaseMqttCallBack() {
                    @Override
                    public void messageArrived(String topic, String message, int messageId) {
                        if (callback != null) {
                            callback.messageArrived(topic, message, messageId);
                        }
                    }

                    @Override
                    public void onSuccess(DlcMqttToken asyncActionToken) {
                        if (!isMqttSuccess) {
                            isMqttSuccess = true;
                        }
                    }

                    @Override
                    public void onFailure(DlcMqttException e) {
                        if (callback != null) {
                            callback.onError(e);
                        }
                    }
                });
    }

    public void intiSerialPort(Context context) {
        GarbageRecyclerBoxCmdProxy.get().init(context, initSerialPortGrbBean,
                initVisionSerialPortGrbBean, mCallback);
    }

    public void reConnect(Context context, InitSerialPortGrbBean initSerialPortGrbBean, InitSerialPortGrbBean initVisionSerialPortGrbBean,
                          GarbageRecyclerBoxCmdCallback tempCallback) {
        GarbageRecyclerBoxCmdProxy.get().reConnect(context, initSerialPortGrbBean, initVisionSerialPortGrbBean, tempCallback);
    }
    public void reConnect(Context context, InitSerialPortGrbBean initSerialPortGrbBean,
                          GarbageRecyclerBoxCmdCallback tempCallback) {
        GarbageRecyclerBoxCmdProxy.get().reConnect(context, initSerialPortGrbBean, tempCallback);
    }

    public void onDestroy() {
        MqttMgr.get().onDestroy();
        GarbageRecyclerBoxCmdProxy.get().close();
    }

}
